package org.chen.scene.service.districtid;

import org.chen.common.scene.dto.SearchResponse;
import org.chen.common.scene.model.SearchRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class CtripDistritIdSearchService {
    
    private final String API_URL = "https://m.ctrip.com/restapi/soa2/19478/getSearchList";
    private final RestTemplate restTemplate;

    public CtripDistritIdSearchService() {
        this.restTemplate = new RestTemplate();
    }
    
    /**
     * 搜索城市信息
     * @param keyword 搜索关键词
     * @return 搜索结果
     */
    public SearchResponse searchCity(String keyword) {
        try {
            // 创建请求参数
            SearchRequest request = new SearchRequest(keyword);
            
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            // 可以根据需要添加其他头信息，如User-Agent等
            headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
            
            // 创建请求实体
            HttpEntity<SearchRequest> httpEntity = new HttpEntity<>(request, headers);
            
            // 发送POST请求
            ResponseEntity<SearchResponse> response = restTemplate.postForEntity(
                API_URL, 
                httpEntity, 
                SearchResponse.class
            );

            return response.getBody();
            
        } catch (Exception e) {
            throw new RuntimeException("请求携程API失败: " + e.getMessage());
        }
    }
}